/*
package com.taiji.dianthus.common;

import com.alibaba.fastjson.JSONObject;
import com.taiji.dianthus.domain.sms.SmsAccount;
import com.taiji.dianthus.domain.sms.SmsInfo;
import com.taiji.dianthus.domain.sms.SmsMoInfo;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.service.sms.SmsAccountService;
import com.taiji.dianthus.service.sms.SmsInfoService;
import com.taiji.dianthus.service.sms.SmsMoInfoService;
import com.taiji.dianthus.service.sys.SysLogService;
import com.taiji.dianthus.service.sys.UserService;
import com.taiji.dianthus.sms.HttpSendSmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.taiji.dianthus.common.Constant.LOG_OPERATION_SAVE;

*/
/**
 * 定时任务
 *//*

@Component
public class ScheduleTask {
    private Logger logger = LoggerFactory.getLogger(ScheduleTask.class);


    @Autowired
    private SmsMoInfoService moInfoService;
    @Autowired
    private HttpSendSmsService httpSendSmsService;
    @Autowired
    private SmsAccountService accountService;

    @Autowired
    private SmsInfoService smsInfoService;
    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private UserService userService;

    */
/**
     * 定时扫描数据库时间 发送短信
     *//*

    @Scheduled(cron = "0 0/5 * * * ?")
    public synchronized void runMessageSendTask() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        System.out.println("定时扫描发送短信===" + new Date());
        List<SmsInfo> smsInfoList = smsInfoService.getTaskMsgByTime(calendar.getTime());
        if (smsInfoList.size() > 0) {
            for (int i = 0; i < smsInfoList.size(); i++) {
                SmsInfo smsInfo = smsInfoList.get(i);
                smsInfo.setCreateTime(new Date());
                smsInfo.setMsgLoc(1);
                BusinessMessage message = smsInfoService.sendDSMS(smsInfo);
                DSysUser user = userService.getById(smsInfo.getSendUserId());
                sysLogService.save(user, LOG_OPERATION_SAVE,
                        null, "定时发送短信息", message.success ? 1 : 0, message.getMsg());
            }
        }

    }

    @Scheduled(cron = "0 0 1 * * ?")
    public synchronized void commitTask() {
        logger.info("将发送中任务置为已发送开始----------" + new Date());
        try {
            List<SmsInfo> list = smsInfoService.getAllByStatus(0);
            for (int i = 0; i < list.size(); i++) {
                SmsInfo smsInfo = list.get(i);
                smsInfoService.updateMsgStatusById(smsInfo.getId(), 1);
            }
            logger.info("将发送中任务置为失败任务结束----------" + new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "0 0 0-23 * * ?")
    public synchronized void retrySendMsg() {
        logger.info("重试次数为0 则查询据当下没有回复的手机号1小时内重发 ；重试次数为1 据当下没有回复的手机号3小时内重发----------" + new Date());
        try {
            Calendar calendar01 = Calendar.getInstance();
            calendar01.setTime(new Date());
            calendar01.add(Calendar.HOUR_OF_DAY, -2);
            Calendar calendar0 = Calendar.getInstance();
            calendar0.setTime(new Date());
            calendar0.add(Calendar.HOUR_OF_DAY, -1);
            //查询重试次数为0并且时间间隔为1小时
            List<SmsInfo> smsInfoList = smsInfoService.getAllByTime(calendar01.getTime(), calendar0.getTime(), 0);
            for (int i = 0; i < smsInfoList.size(); i++) {
                SmsInfo smsInfo = smsInfoList.get(i);
                List<SmsMoInfo> moInfoList = moInfoService.getBySmsInfoId(smsInfo.getId());
                String mobiles = getRetryMobiles(smsInfo, moInfoList);
                if (!StringUtils.isEmpty(mobiles)) {
                    SmsAccount smsAccount = accountService.getById(smsInfo.getSmsAccountId());
                    SmsInfo retrySmsInfo = new SmsInfo();
                    retrySmsInfo.setReceiveUserPhone(mobiles);
                    retrySmsInfo.setMsgContent(smsInfo.getMsgContent());
                    JSONObject obj = httpSendSmsService.sendDSMS(smsInfo, smsAccount);
                    if (obj != null) {
                        if (obj.getBoolean("success")) {
                            String smsGroup = obj.getString("msgGroup");
                            smsInfoService.updateRetryCount1(smsInfo.getId(), 1, smsGroup);
                        }
                    }
                }
            }
            //查询重试次数为1并且时间间隔为3小时
            Calendar start = Calendar.getInstance();
            start.setTime(new Date());
            start.add(Calendar.HOUR_OF_DAY, -4);
            Calendar end = Calendar.getInstance();
            end.setTime(new Date());
            end.add(Calendar.HOUR_OF_DAY, -3);
            List<SmsInfo> retry2list = smsInfoService.getAllByTime(start.getTime(), end.getTime(), 1);
            for (int i = 0; i < retry2list.size(); i++) {
                SmsInfo smsInfo = retry2list.get(i);
                List<SmsMoInfo> moInfoList = moInfoService.getBySmsInfoId(smsInfo.getId());
                String mobiles = getRetryMobiles(smsInfo, moInfoList);
                if (!StringUtils.isEmpty(mobiles)) {
                    SmsAccount smsAccount = accountService.getById(smsInfo.getSmsAccountId());
                    SmsInfo retrySmsInfo = new SmsInfo();
                    retrySmsInfo.setReceiveUserPhone(mobiles);
                    retrySmsInfo.setMsgContent(smsInfo.getMsgContent());
                    JSONObject obj = httpSendSmsService.sendDSMS(smsInfo, smsAccount);
                    if (obj != null) {
                        if (obj.getBoolean("success")) {
                            String smsGroup = obj.getString("msgGroup");
                            smsInfoService.updateRetryCount2(smsInfo.getId(), 2, smsGroup);
                        }
                    }
                }
            }
            logger.info("任务结束----------" + new Date());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getRetryMobiles(SmsInfo smsInfo, List<SmsMoInfo> moInfoList) {
        String retryMobiles = null;
        StringBuilder mobileBuilder = new StringBuilder();
        String[] userPhones = smsInfo.getReceiveUserPhone().split(",");
        if (moInfoList.size() > 0 && moInfoList.size() < userPhones.length) {
            for (int i = 0; i < userPhones.length; i++) {
                boolean flag = false;
                for (int j = 0; j < moInfoList.size(); j++) {
                    SmsMoInfo moInfo = moInfoList.get(j);
                    if (moInfo.getMobile().equals(userPhones[i])) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    mobileBuilder.append(userPhones[i]).append(",");
                }
            }
        } else if (moInfoList.size() <= 0) {
            for (int i = 0; i < userPhones.length; i++) {
                mobileBuilder.append(userPhones[i]).append(",");
            }
        }
        if (mobileBuilder.toString().endsWith(",")) {
            retryMobiles = mobileBuilder.toString().substring(0, mobileBuilder.toString().length() - 1);
        }
        return retryMobiles;
    }
}
*/
